/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.datarecorders;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Michael Ferry
 */
public class WWControlSensorLogFile extends AbstractDataRecorder {
    private BufferedWriter LogWriter;
    private final SimpleDateFormat LogEntryDateFormatter;
    
    public  WWControlSensorLogFile(String Path, SimpleDateFormat 
            FileDateFormatter, SimpleDateFormat LogEntryDateFormatter){
        this.LogEntryDateFormatter = LogEntryDateFormatter;
        Date currTime = new Date(); 
        String ServerLogFilePath = Path + FileDateFormatter.format(currTime) + "_SensorLogFile.txt";
        try {
            LogWriter = new BufferedWriter(new FileWriter(ServerLogFilePath));
            System.out.println("Sensor Log file path: " + ServerLogFilePath);
            //RecordLogEntry("Server started");  
            
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    @Override
    public void InsertSensorRecord(int SerialNumber, int SensorIndex, int sval,
            double cval, int ratiometric, int sensitivity, double DYNR, int Attached) {
        Date currTime = new Date();
       
        String LogEntry = "\t" + SerialNumber + "\t" + SensorIndex + "\t" + sval +
                "\t" + cval + "\t" + ratiometric + "\t" + sensitivity + "\t" + DYNR + "\t" + Attached;

        String OutputString = LogEntryDateFormatter.format(currTime) + ": " + LogEntry;
        WriteLogEntry(OutputString);
    }
    
    @Override
    public void InsertSensorRecord(Date currtime,int SerialNumber, int SensorIndex, int sval,
            double cval, int ratiometric, int sensitivity, double DYNR, int Attached) {
       
        String LogEntry = "\t" + SerialNumber + "\t" + SensorIndex + "\t" + sval +
                "\t" + cval + "\t" + ratiometric + "\t" + sensitivity + "\t" + DYNR + "\t" + Attached;

        String OutputString = LogEntryDateFormatter.format(currtime) + ": " + LogEntry;      
        WriteLogEntry(OutputString);
    }
    
    @Override
    public void InsertSensorRecordNA(int SerialNumber){
        Date currTime = new Date();
        String LogEntry = "\t" + SerialNumber + "\t" + "Not Attached";
        String OutputString = LogEntryDateFormatter.format(currTime) + ": " + LogEntry;       
        WriteLogEntry(OutputString);
    }
    @Override
    public void InsertDORecord(int SerialNumber, int DOIndex, boolean state){
        Date currTime = new Date();
        String LogEntry = "\t" + SerialNumber + "\t" + "Digital Output: " + DOIndex + "\t" + state;
        String OutputString = LogEntryDateFormatter.format(currTime) + ": " + LogEntry;       
        WriteLogEntry(OutputString);
    }
    @Override
    public void InsertDIRecord(int SerialNumber, int DIIndex, boolean state){
        Date currTime = new Date();
        String LogEntry = "\t" + SerialNumber + "\t" + "Digital Input: " + DIIndex + "\t" + state;
        String OutputString = LogEntryDateFormatter.format(currTime) + ": " + LogEntry;       
        WriteLogEntry(OutputString);
    }
    @Override
    public void InsertAttachDetachRecord(int SerialNumber, boolean EventType) {
        Date currTime = new Date();
        String LogEntry = "\t" + SerialNumber + "\t" + "Attach/Detach" + "\t" + EventType;
        String OutputString = LogEntryDateFormatter.format(currTime) + ": " + LogEntry;       
        WriteLogEntry(OutputString);
    }
    private void WriteLogEntry(String OutputString){
        try {
            LogWriter.write(OutputString);
            LogWriter.newLine();
            LogWriter.flush();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    @Override
    public void close() {
        try {
            LogWriter.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}
